package com.yunlong.softpark.service.impl;

import com.yunlong.softpark.dto.CommentCheckDto;
import com.yunlong.softpark.entity.CommentEntity;
import com.yunlong.softpark.form.CommentForm;
import com.yunlong.softpark.mapper.CommentMapper;
import com.yunlong.softpark.redis.RedisRepository;
import com.yunlong.softpark.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Autowired
    RedisRepository redisRepository;

    /**
     * 根据colomn获取评论信息
     *
     * @param columnId
     * @return
     * @throws ParseException
     */
    @Override
    public List<CommentCheckDto> getCommentData(String columnId) throws ParseException {
        List<CommentEntity> list =commentMapper.selectByColumnId(columnId);
        List<CommentCheckDto> checkDtos = new ArrayList<>();
        try {
            Collections.sort(list, new Comparator<CommentEntity>() {
                @Override
                public int compare(CommentEntity commentEntity, CommentEntity t1) {
                    int flag = commentEntity.getTime().compareTo(t1.getTime());
                    if (flag == -1){
                        return flag = 1;
                    }else if (flag == 1){
                        return flag = -1;
                    }else {
                        return flag;
                    }
                }
            });
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for(CommentEntity commentEntity:list){
                CommentCheckDto check = new CommentCheckDto();
                if (commentEntity.getVerify()!=0){
                    check.setUser_id( commentEntity.getUserId());
                    check.setContent(commentEntity.getContent());
                    check.setDate(
                            sdf.format(commentEntity.getTime()));
                    checkDtos.add(check);
                }
            }

        }catch (NullPointerException e){
            System.out.println("查询失败或者数据空信息空！");
        }
        return checkDtos;
    }

    /**
     * 根据传入的评论的相关信息添加到库中
     *
     * @param commentForm,token
     */
    @Override
    public String insertCommentData(CommentForm commentForm, String userId) {
        CommentEntity commentEntity = new CommentEntity();
        /**
         * 这里如果token过期那么就会无法获取到userID就会爆出异常
         * 这个异常可以在后台打印但是无法直接的返回给前端
         * 所以这里应该需要抛出这个异常，然后将这个异常留给一个大的处理结构去处理
         * 统一处理这一类的token过期异常
         */
        commentEntity.setUserId(userId);
        commentEntity.setColumnId(commentForm.getColumnId());
        commentEntity.setContent(commentForm.getContent());
        commentEntity.setCommentId(UUID.randomUUID().toString());
        commentEntity.setVerify(0);
        Date date = new Date();
        commentEntity.setTime(date);

        commentMapper.insertNewComment(commentEntity);

        return commentEntity.getCommentId();
    }
}